<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<canvas width="1000" height="600" id="cv"></canvas>
</body>

</html>
<script>

    let cv = document.getElementById("cv");
    var ctx = cv.getContext('2d');

    class DrawLine {
        isMouseOn;
        draw(point1, point2, e) {
            ctx.beginPath();
            ctx.lineWidth = 6;
            ctx.moveTo(point1.x, point1.y)
            ctx.lineTo(point2.x, point2.y)
            ctx.stroke();
            this.isMouseOn = ctx.isPointInStroke(e.clientX, e.clientY);   // 这里我的canvas就在左上角,如果不是需要减去canvas距离屏幕左上方的距离哦
        }
    }

    let line1 = new DrawLine();
    line1.draw({ x: 10, y: 10 }, { x: 400, y: 400 }, { x: -1, y: -1 })
    let line2 = new DrawLine();
    line2.draw({ x: 100, y: 0 }, { x: 100, y: 400 }, { x: -1, y: -1 })

    window.onmousemove = function (e) {
        line1.draw({ x: 10, y: 10 }, { x: 400, y: 400 }, e)
        line2.draw({ x: 100, y: 0 }, { x: 100, y: 400 }, e)
    }


</script>
<style>
    * {
        padding: 0;
        margin: 0;
    }

    canvas {
        /* border: 1px solid; */
    }
</style>